package com.hengdu.clean.service;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CleanTextService {

	public void clean(String inputFilePath, String saveFilePath, String regeFilePath) {

		List<String> fileList = new ArrayList<>();

		// 读取所有的文件
		this.readerFiles(fileList, inputFilePath);

		// 读取所有的正则表达式
		List<String> regeList = this.readerRege(regeFilePath);
		if (regeList == null || regeList.size() == 0) {
			System.err.println("正则表达式为空....");
		} else {

			// 循环文件,循环正则
			for (String path : fileList) {

				List<String> file = readerRege(path);
				if (null == file || file.size() == 0) {
					System.err.println(path + "------文件内容为空......");
					continue;
				}

				// 循环文本
				for (String str : file) {

					// 循环正则
					for (String rege : regeList) {

						if (rege.contains("split")) {
							String[] arr = rege.split("split");
							if (arr.length == 2) {

								str = str.replace(rege.split("split")[0], rege.split("split")[1]);
							} else if (arr.length == 1) {

								str = str.replace(rege.split("split")[0], "");
							}
						} else {
							str = regEx(str, rege);
						}
					}

					// 保存文件
					saveFile(str, new File(path).getName(), saveFilePath);
				}
			}
		}
		fileList.forEach(System.out::println);
	}

	public void saveFile(String str, String fileName, String filePath) {

		isFileExist(filePath);

		File file = new File(filePath + "/" + fileName);

		BufferedWriter bw = null;
		FileOutputStream fos = null;

		try {

			fos = new FileOutputStream(file, true);
			bw = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"));

			bw.write(str);
			bw.newLine();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != bw) {
					bw.close();
				}
				if (null != fos) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public String regEx(String str, String rege) {

		Pattern pattern = Pattern.compile(rege);
		Matcher matcher = pattern.matcher(str);

		return matcher.replaceAll("").trim();
	}

	/**
	 * 递归查找路径下的所有文件
	 * 
	 * @param item
	 * @param inputFilePath
	 * @return
	 */
	private List<String> readerFiles(List<String> item, String inputFilePath) {

		File file = new File(inputFilePath);
		File[] list = file.listFiles();
		for (File fl : list) {
			if (!fl.isDirectory()) {
				item.add(fl.getAbsolutePath());
			} else {
				readerFiles(item, fl.getAbsolutePath());
			}
		}
		return item;
	}

	/**
	 * 读取所有的正则表达式
	 * 
	 * @param regeFilePath
	 * @return
	 */
	public List<String> readerRege(String regeFilePath) {

		File file = new File(regeFilePath);
		if (!file.exists()) {
			System.err.println("读取的文件不存在......");
			return null;
		}

		List<String> regeList = new ArrayList<>();

		BufferedReader br = null;
		try {

			br = new BufferedReader(new FileReader(file));
			String line;
			while ((line = br.readLine()) != null) {
				regeList.add(line.trim());
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != br) {
					br.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return regeList;
	}

	/**
	 * 目录创建
	 * 
	 * @param filePath
	 */
	private void isFileExist(String filePath) {

		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
	}

}
